package com.pet.platform.controller.admin;

import com.pet.platform.common.PageResult;
import com.pet.platform.common.Result;
import com.pet.platform.dto.OrderDTO;
import com.pet.platform.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RestController
@RequestMapping("/api/admin/orders")
@RequiredArgsConstructor
@PreAuthorize("hasRole('ADMIN')")
public class AdminOrderController {
    private final OrderService orderService;
    private static final Logger log = LoggerFactory.getLogger(AdminOrderController.class);

    @GetMapping
    public Result getOrders(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String orderNo,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        
        log.info("查询订单列表 - 关键词: {}, 订单号: {}, 用户名: {}, 状态: {}, 开始日期: {}, 结束日期: {}", 
                keyword, orderNo, username, status, startDate, endDate);
                
        PageResult<OrderDTO> pageResult = orderService.advancedSearchOrders(
                keyword, orderNo, username, status, startDate, endDate, pageNum, pageSize);
                
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    public Result<OrderDTO> getOrderById(@PathVariable Long id) {
        return Result.success(orderService.getOrderById(id));
    }

    @PutMapping("/{id}/status")
    public Result<OrderDTO> updateOrderStatus(
            @PathVariable Long id,
            @RequestParam String status) {
        return Result.success(orderService.updateOrderStatus(id, status));
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteOrder(@PathVariable Long id) {
        orderService.deleteOrder(id);
        return Result.success();
    }
} 